﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ManagerViewStaff : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetAllStudent();
            GetAllCompetition();
            DefaultUpdateCompetition();
            DefaultMarkDesign();
            GetDropDownListCompetition();
        }
    }

    private void DefaultUpdateCompetition()
    {
        DefaultDetailsUpdateCompetition();
    }

    private void DefaultDetailsUpdateCompetition()
    {
        ManagerBUS manager = new ManagerBUS();
        grvUpdateCompetition.SelectRow(0);
        GridViewRow row = grvUpdateCompetition.SelectedRow;
        int comId=Convert.ToInt32(grvUpdateCompetition.DataKeys[row.RowIndex].Value.ToString());
        Competition c = manager.searchCompetitionById(comId);
        txtNameCompetition.Text = c.Name;
        txtAwardsCompetition.Text = c.Award;
        txtPostDateComeptition.Text = c.PostDate;
        txtStartDateCompetition.Text = c.StartDate;
        txtEndDateCompetition.Text = c.EndDate;
        txtDescriptionUpdateCompetition.Text = c.Description;
        txtQuotationUpdateCompetition.Text = c.Quotation;
    }

    private void GetAllCompetition()
    {
        ManagerBUS manager = new ManagerBUS();
        /*
        DataSet ds = manager.getAllCompetition();
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
            return;
        }
         */
        grvViewCompetition.DataSource = manager.getAllCompetition().Tables[0];
        grvViewCompetition.DataBind();
        grvUpdateCompetition.DataSource = manager.getAllCompetition().Tables[0];
        grvUpdateCompetition.DataBind();
    }

    private void GetAllStudent()
    {
        StaffBUS staff = new StaffBUS();
        grvStudents.DataSource = staff.getAllStudent().Tables[0];
        grvStudents.DataBind();
    }

    private void DefaultMarkDesign()
    {
        DefaultTableMarkDesign();
        DefaultDetailsMarkDesign();
    }

    private void DefaultDetailsMarkDesign()
    {
        StaffBUS staff = new StaffBUS();
        grvMarkDesign.SelectRow(0);
        GridViewRow row = grvMarkDesign.SelectedRow;
        Design d1 = new Design();
        d1.Id = int.Parse(grvMarkDesign.DataKeys[row.RowIndex].Value.ToString());
        Design d = staff.searchDesign(d1);
        lblShownameStudent.Text = d.St.Fullname;
        lblShowPostDate.Text = d.PostDate;
        txtShowDescriptionDesgin.Text = d.Description;
        lblShowQuotationDesign.Text = d.Quotation;
    }

    private void DefaultTableMarkDesign()
    {
        ManagerBUS manager = new ManagerBUS();
        DataTable dt = manager.getAllCompetition().Tables[0];
        StaffBUS staff = new StaffBUS();
        Competition c = new Competition();
        c.ComId = Convert.ToInt32(dt.Rows[0][0].ToString());
        DataSet ds = staff.getAllDesignOnCompetition(c);
        if (ds == null)
        {
            //Response.Write("<script>alert('No Design on competition: " + dt.Rows[0][1].ToString() + "')</script>");
            return;
        }
        grvMarkDesign.DataSource = ds.Tables[0];
        grvMarkDesign.DataBind();
    }

    private void GetDropDownListCompetition()
    {
        ManagerBUS manager = new ManagerBUS();
        DataTable dt=manager.getAllCompetition().Tables[0];
        listCompetition.DataSource =dt ;
        listCompetition.DataTextField = "name";
        listCompetition.DataValueField = "comId";
        listCompetition.DataBind();
    }

    protected void listCompetition_SelectedIndexChanged(object sender, EventArgs e)
    {
        int comId = Convert.ToInt32(listCompetition.SelectedValue);
        StaffBUS staff = new StaffBUS();
        Competition c = new Competition();
        c.ComId = comId;
        DataSet ds = staff.getAllDesignOnCompetition(c);
        if (ds == null)
        {
            //Response.Write("<script>alert('No Design on competition: " + comId + "')</script>");
            return;
        }
        grvMarkDesign.DataSource = ds.Tables[0];
        grvMarkDesign.DataBind();
    }

    private void GetDesignsOnCompetition(Competition c)
    {
        StaffBUS staff = new StaffBUS();
        DataSet ds = staff.getAllDesignOnCompetition(c);
        if (ds == null)
        {
            //Response.Write("<script>alert('No Design on competition: "+name+"')</script>");
            return;
        }
        grvMarkDesign.DataSource = ds.Tables[0];
        grvMarkDesign.DataBind();
    }

    protected void grvMarkDesign_SelectedIndexChanged(object sender, EventArgs e)
    {
        StaffBUS staff = new StaffBUS();
        GridViewRow row = grvMarkDesign.SelectedRow;
        Design d1 = new Design();
        d1.Id = int.Parse(grvMarkDesign.DataKeys[row.RowIndex].Value.ToString());
        Design d = staff.searchDesign(d1);
        lblShownameStudent.Text = d.St.Fullname;
        lblShowPostDate.Text = d.PostDate;
        txtShowDescriptionDesgin.Text = d.Description;
        lblShowQuotationDesign.Text = d.Quotation;
    }

    //Chua lam Login
    protected void btnMarkDesign_Click(object sender, EventArgs e)
    {
        StaffBUS staff = new StaffBUS();
        GridViewRow row = grvMarkDesign.SelectedRow;
        Design d1 = new Design();
        d1.Id = int.Parse(grvMarkDesign.DataKeys[row.RowIndex].Value.ToString());
        Design d = staff.searchDesign(d1);
        Mark m = new Mark();
        m.Design = d;

        //............................
    }
    protected void btnSeachStudent_Click(object sender, EventArgs e)
    {
        StaffBUS staff = new StaffBUS();
        string name = txtSeachStudent.Text;
        /*
        DataSet ds = manager.searchCompetition(name);
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
            return;
        }
         */
        grvStudents.DataSource = staff.searchStudent(name).Tables[0];
        grvStudents.DataBind();
    }
    protected void grvStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvStudents.PageIndex = e.NewPageIndex;
        GetAllStudent();
    }
    protected void grvViewCompetition_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(grvViewCompetition.DataKeys[e.RowIndex].Value.ToString());
        ManagerBUS dt = new ManagerBUS();
        if (dt.delCompetition(id))
        {
            grvViewCompetition.EditIndex = -1;
            Response.Write("<script>alert('One Competiton deleted')</script>");
            GetAllCompetition();
        }
    }
    protected void btnSearchViewCompetition_Click(object sender, EventArgs e)
    {
        ManagerBUS manager = new ManagerBUS();
        string name = txtSearchViewCompetition.Text;
        /*
        DataSet ds = manager.searchCompetition(name);
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
            return;
        }
         */
        grvViewCompetition.DataSource = manager.searchCompetition(name).Tables[0];
        grvViewCompetition.DataBind();
    }
    protected void grvViewCompetition_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvViewCompetition.PageIndex = e.NewPageIndex;
        GetAllCompetition();
    }
    protected void btnUpdateCompetition_Click(object sender, EventArgs e)
    {
        ManagerBUS manager = new ManagerBUS();
        GridViewRow row = grvUpdateCompetition.SelectedRow;
        int comId = Convert.ToInt32(grvUpdateCompetition.DataKeys[row.RowIndex].Value.ToString());
        Competition c = manager.searchCompetitionById(comId);
        c.Name = txtNameCompetition.Text;
        c.Award = txtAwardsCompetition.Text;
        c.PostDate = txtPostDateComeptition.Text;
        c.StartDate = txtStartDateCompetition.Text;
        c.EndDate = txtEndDateCompetition.Text;
        c.Description = txtDescriptionUpdateCompetition.Text;
        c.Quotation = txtQuotationUpdateCompetition.Text;
        if (manager.updateCompetition(c))
        {
            Response.Write("<script>alert('One Competition updated')</script>");
            return;
        }
        else
        {
            Response.Write("<script>alert('This error')</script>");
            return;
        }
        
    }
    protected void btnSearchUpdateCompetition_Click(object sender, EventArgs e)
    {
        ManagerBUS manager = new ManagerBUS();
        string name = txtSearchUpdateCompetition.Text;
        /*
        DataSet ds = manager.searchCompetition(name);
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
            return;
        }
         */
        grvViewCompetition.DataSource = manager.searchCompetition(name).Tables[0];
        grvViewCompetition.DataBind();
    }

    protected void grvUpdateCompetition_SelectedIndexChanged(object sender, EventArgs e)
    {
        ManagerBUS manager = new ManagerBUS();
        GridViewRow row = grvUpdateCompetition.SelectedRow;
        int comId = Convert.ToInt32(grvUpdateCompetition.DataKeys[row.RowIndex].Value.ToString());
        Competition c = manager.searchCompetitionById(comId);
        txtNameCompetition.Text = c.Name;
        txtAwardsCompetition.Text = c.Award;
        txtPostDateComeptition.Text = c.PostDate;
        txtStartDateCompetition.Text = c.StartDate;
        txtEndDateCompetition.Text = c.EndDate;
        txtDescriptionUpdateCompetition.Text = c.Description;
        txtQuotationUpdateCompetition.Text = c.Quotation;
    }
}